package com.site.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.site.base.dao.PageBaseDao;
import com.site.entity.HuntSite;

/**
 * A data access object (DAO) providing persistence and search support for
 * HuntSite entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.site.entity.HuntSite
 * @author MyEclipse Persistence Tools
 */
@Repository
public class HuntSiteDao extends PageBaseDao<HuntSite> {
	private static final Log log = LogFactory.getLog(HuntSiteDao.class);
	// property constants
	public static final String TYPE = "type";
	public static final String PR = "pr";
	public static final String LANG = "lang";
	public static final String DESCRIPTION = "description";
	public static final String PRIORITY = "priority";

	public void save(HuntSite transientInstance) {
		log.debug("saving HuntSite instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(HuntSite persistentInstance) {
		log.debug("deleting HuntSite instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public HuntSite findById(java.lang.String id) {
		log.debug("getting HuntSite instance with id: " + id);
		try {
			HuntSite instance = (HuntSite) getSession().get(
					"com.site.db.HuntSite", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<HuntSite> findByExample(HuntSite instance) {
		log.debug("finding HuntSite instance by example");
		try {
			List<HuntSite> results = getSession().createCriteria("com.site.db.HuntSite")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<HuntSite> findByProperty(String propertyName, Object value) {
		log.debug("finding HuntSite instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from HuntSite as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByPr(Object pr) {
		return findByProperty(PR, pr);
	}

	public List findByLang(Object lang) {
		return findByProperty(LANG, lang);
	}

	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List findByPriority(Object priority) {
		return findByProperty(PRIORITY, priority);
	}

	public List<HuntSite> findAll() {
		log.debug("finding all HuntSite instances");
		try {
			String queryString = "from HuntSite";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public HuntSite merge(HuntSite detachedInstance) {
		log.debug("merging HuntSite instance");
		try {
			HuntSite result = (HuntSite) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(HuntSite instance) {
		log.debug("attaching dirty HuntSite instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(HuntSite instance) {
		log.debug("attaching clean HuntSite instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}